/*
 * Copyright 2022-2029 heshuixing(https://gitee.com/heshuixing)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mercury.ec.framework.air.core.client.impl.qunar.convert;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.mercury.ec.framework.air.core.client.dto.search.flight.AirFlightRespDTO;
import com.mercury.ec.framework.air.core.client.dto.search.flight.AirFlightStopRespDTO;
import com.mercury.ec.framework.air.core.client.dto.search.flight.AirSearchFlightReqDTO;
import com.mercury.ec.framework.air.core.client.dto.search.flight.AirSearchFlightRespDTO;
import com.mercury.ec.framework.air.core.client.dto.search.price.AirPriceRespDTO;
import com.mercury.ec.framework.air.core.client.dto.search.price.AirSearchPriceReqDTO;
import com.mercury.ec.framework.air.core.client.dto.search.price.AirSearchPriceRespDTO;
import com.mercury.ec.framework.air.core.client.impl.qunar.dto.searchflight.*;
import com.mercury.ec.framework.air.core.client.impl.qunar.dto.searchprice.QunarDirectPriceResultDTO;
import com.mercury.ec.framework.air.core.client.impl.qunar.dto.searchprice.QunarSearchPriceReqDTO;
import com.mercury.ec.framework.air.core.client.impl.qunar.dto.searchprice.QunarSearchPriceRespDTO;
import com.mercury.ec.framework.air.core.client.impl.qunar.dto.searchprice.QunarVendorDTO;
import com.mercury.ec.framework.air.core.client.impl.qunar.util.QunarUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 去哪儿参数转换
 *
 * @author hesx
 */
public class QunarConvert {

    /**
     * 转换去哪儿航班查询请求参数
     *
     * @param reqDTO
     * @return
     */
    public static QunarSearchFlightReqDTO convertQunarSearchFlightReqDTO(AirSearchFlightReqDTO reqDTO) {
        QunarSearchFlightReqDTO searchFlightReqDTO = new QunarSearchFlightReqDTO();
        searchFlightReqDTO.setDepCityName(reqDTO.getDepart());
        searchFlightReqDTO.setArrCityName(reqDTO.getArrive());
        searchFlightReqDTO.setDepDate(reqDTO.getFlightDate().toString());
        return searchFlightReqDTO;
    }

    /**
     * 转换标准航班查询响应参数
     *
     * @param channelId
     * @param channelName
     * @param channelType
     * @param respDTO
     * @return
     */
    public static AirSearchFlightRespDTO convertAirSearchFlightRespDTO(Long channelId, String channelName, Integer channelType, QunarSearchFlightRespDTO respDTO) {
        AirSearchFlightRespDTO airSearchFlightRespDTO = new AirSearchFlightRespDTO();
        QunarDirectFlightInfoDTO directFlightResult = respDTO.getDirectFlightResult();
        if (directFlightResult != null) {
            airSearchFlightRespDTO.setFlightDate(LocalDateTimeUtil.parseDate(directFlightResult.getDepTime().getIntValue().toString(), "yyyyMMdd"));
            airSearchFlightRespDTO.setDepart(directFlightResult.getGeographyInfo().getDepCity().getCity());
            airSearchFlightRespDTO.setDepartCityName(directFlightResult.getGeographyInfo().getDepCity().getCityZh());
            airSearchFlightRespDTO.setArrive(directFlightResult.getGeographyInfo().getArrCity().getCity());
            airSearchFlightRespDTO.setArriveCityName(directFlightResult.getGeographyInfo().getArrCity().getCityZh());
            airSearchFlightRespDTO.setUpdateTime(LocalDateTime.now());
            airSearchFlightRespDTO.setTotal(directFlightResult.getTotal());
            List<AirFlightRespDTO> flights = new ArrayList<>();
            for (QunarListsDTO list : directFlightResult.getList()) {
                AirFlightRespDTO airFlightRespDTO = new AirFlightRespDTO();
                airFlightRespDTO.setChannelId(channelId);
                airFlightRespDTO.setChannelName(channelName);
                airFlightRespDTO.setChannelType(channelType);
                airFlightRespDTO.setAirlineNo(list.getCarrier());
                airFlightRespDTO.setAirlineName(list.getShortName());
                airFlightRespDTO.setCarrierAirlineName(list.getMainCarrierShortName());
                airFlightRespDTO.setFlightNo(list.getFlightNo().substring(2));
                if (StrUtil.isNotBlank(list.getMainCarrier())) {
                    airFlightRespDTO.setCarrierAirlineNo(list.getMainCarrier().substring(0, 2));
                    airFlightRespDTO.setCarrierFlightNo(list.getFlightNo().substring(2));
                }
                airFlightRespDTO.setDepart(list.getDepAirportCode());
                airFlightRespDTO.setDepartAirportName(list.getDepAirport());
                airFlightRespDTO.setArrive(list.getArrAirportCode());
                airFlightRespDTO.setArriveAirportName(list.getArrAirport());
                airFlightRespDTO.setDepartTerminal(list.getDepTower());
                airFlightRespDTO.setArriveTerminal(list.getArrTower());
                airFlightRespDTO.setDepartTime(list.getBtime());
                airFlightRespDTO.setArriveTime(list.getEtime());
                airFlightRespDTO.setAirModel(list.getPlanetype());
                airFlightRespDTO.setAirModelName(list.getFlightTypeFullName());
                airFlightRespDTO.setHaveFood(list.getMeal());
                airFlightRespDTO.setFoodType(list.getMealType());
                airFlightRespDTO.setShare(list.getCodeshare());
                airFlightRespDTO.setThrough(list.getPTrip());
                airFlightRespDTO.setStop(list.getStops());
                airFlightRespDTO.setPunctualityRate(QunarUtils.getPunctualityRate(list.getCorrect()));
                airFlightRespDTO.setDistance(list.getDistance());
                airFlightRespDTO.setFlightTimes(list.getFlightTimes());
                airFlightRespDTO.setAirportTax(list.getArf());
                airFlightRespDTO.setOilTax(list.getTof());
                airFlightRespDTO.setDiscount(list.getDiscount());
                airFlightRespDTO.setLowestPrice(list.getMinPrice());
                List<AirFlightStopRespDTO> stops = new ArrayList<>();
                for (String cityName : list.getStopCitys()) {
                    AirFlightStopRespDTO airFlightStopRespDTO = new AirFlightStopRespDTO();
                    airFlightStopRespDTO.setStopStartDate(null);
                    airFlightStopRespDTO.setStopEndDate(null);
                    airFlightStopRespDTO.setStopStartTime(null);
                    airFlightStopRespDTO.setStopEndTime(null);
                    airFlightStopRespDTO.setStopCityName(cityName);
                    airFlightStopRespDTO.setStopAirportNo(null);
                    airFlightStopRespDTO.setStopTerminal(null);
                    stops.add(airFlightStopRespDTO);
                }
                airFlightRespDTO.setStops(stops);
                airFlightRespDTO.setFlightDays(QunarUtils.getFlightDays(airFlightRespDTO.getDepartTime(), airFlightRespDTO.getArriveTime()));
                flights.add(airFlightRespDTO);
            }
            airSearchFlightRespDTO.setFlights(flights);
        }
        return airSearchFlightRespDTO;
    }

    /**
     * 转换去哪儿运价查询请求参数
     *
     * @param reqDTO
     * @return
     */
    public static QunarSearchPriceReqDTO convertQunarSearchPriceReqDTO(AirSearchPriceReqDTO reqDTO) {
        QunarSearchPriceReqDTO searchPriceReqDTO = new QunarSearchPriceReqDTO();
        searchPriceReqDTO.setDepCityName(reqDTO.getDepart());
        searchPriceReqDTO.setArrCityName(reqDTO.getArrive());
        searchPriceReqDTO.setCode(reqDTO.getAirlineNo() + reqDTO.getFlightNo());
        searchPriceReqDTO.setDepDate(reqDTO.getFlightDate().toString());
        return searchPriceReqDTO;
    }

    /**
     * 转换标准运价查询响应参数
     *
     * @param channelId
     * @param channelName
     * @param channelType
     * @param respDTO
     * @return
     */
    public static AirSearchPriceRespDTO convertAirSearchPriceRespDTO(Long channelId, String channelName, Integer channelType, QunarSearchPriceRespDTO respDTO) {
        AirSearchPriceRespDTO airSearchPriceRespDTO = new AirSearchPriceRespDTO();
        QunarDirectPriceResultDTO directPriceResult = respDTO.getDirectPriceResult();
        if (directPriceResult != null) {
            airSearchPriceRespDTO.setUpdateTime(LocalDateTime.now());
            airSearchPriceRespDTO.setTotal(directPriceResult.getVendors().size());
            List<AirPriceRespDTO> prices = new ArrayList<>();
            for (QunarVendorDTO vendor : directPriceResult.getVendors()) {
                AirPriceRespDTO airPriceRespDTO = new AirPriceRespDTO();
                airPriceRespDTO.setChannelId(channelId);
                airPriceRespDTO.setChannelName(channelName);
                airPriceRespDTO.setChannelType(channelType);
                airPriceRespDTO.setProductName(vendor.getTagName());
                airPriceRespDTO.setProductType(vendor.getPmark());
                airPriceRespDTO.setProductProperty(vendor.getTagProperty());
                airPriceRespDTO.setPolicyId(vendor.getPid());
                airPriceRespDTO.setPolicyType(vendor.getPolicyType());
                airPriceRespDTO.setCabin(vendor.getCabin());
                airPriceRespDTO.setCabinCount(vendor.getCabinCount());
                airPriceRespDTO.setCabinType(QunarUtils.getCabinType(vendor.getCabinType()));
                airPriceRespDTO.setCabinTypeName(vendor.getCabinTypeViewName());
                airPriceRespDTO.setPublicPrice(vendor.getBarePrice());
                airPriceRespDTO.setPrice(vendor.getBarePrice());
                airPriceRespDTO.setPriceSource(null); // todo 去哪儿没有该参数
                airPriceRespDTO.setDiscount(vendor.getDiscount());
                airPriceRespDTO.setDiscountPrice(vendor.getBarePrice());
                airPriceRespDTO.setChildPrice(vendor.getChildPrice());
                airPriceRespDTO.setChildPriceSource(null); // todo 去哪儿没有该参数
                airPriceRespDTO.setBabyPrice(vendor.getInfantPrice());
                airPriceRespDTO.setBabyPriceSource(null); // todo 去哪儿没有该参数
                airPriceRespDTO.setBookingKey(vendor.getBookingParamKey());
                airPriceRespDTO.setBookingSecret(vendor.getBusinessExt());
                prices.add(airPriceRespDTO);
            }
            airSearchPriceRespDTO.setPrices(prices);
        }
        return airSearchPriceRespDTO;
    }
}
